const express = require('express');
const path = require('path');
const bodyParser = require('body-parser');

let app = express();
//创建application/json解析
app.use(bodyParser.json());
//创建application/x-www-form-urlencoded
app.use(bodyParser.urlencoded({ extended: false }));

// 静态文件
app.use('/upload', express.static(path.join(__dirname, 'upload')));

app.all('*', function(req, res, next) {
    res.header("Access-Control-Allow-Origin", "*");
    res.header("Access-Control-Allow-Headers", "Content-Type,Content-Length, Authorization, Accept,X-Requested-With");
    res.header("Access-Control-Allow-Methods", "PUT,POST,GET,DELETE,OPTIONS");
    res.header("X-Powered-By", ' 3.2.1')
    if (req.method == "OPTIONS") res.send(200); /*让options请求快速返回*/
    else next();
});

/**
 * 后台系统接口
 */
// 类型功能接口
const typeRouters = require('./routers/system/type');
app.use('/system/type/add', typeRouters.add);
app.use('/system/type/search', typeRouters.search);
app.use('/system/type/edit', typeRouters.edit);
app.use('/system/type/delete', typeRouters.delete);

// 上传功能接口
const uploadRouters = require('./controller/system/upload');
app.use('/system/upload/wechatImagesUpload', uploadRouters.wechatImagesUpload);
app.use('/system/upload/coverImagesUpload', uploadRouters.coverImagesUpload);
app.use('/system/upload/articleImagesUpload', uploadRouters.articleImagesUpload);

// 配置项功能接口
const configureRouters = require('./routers/system/configure');
app.use('/system/configure/search', configureRouters.search);

// 用户功能接口
const userRouters = require('./routers/system/user');
app.use('/system/user/search', userRouters.search);
app.use('/system/user/add', userRouters.add);
app.use('/system/user/delete', userRouters.delete);
app.use('/system/user/edit', userRouters.edit);

// 文章功能接口
const articleRouters = require('./routers/system/article');
app.use('/system/article/add', articleRouters.add);
app.use('/system/article/delete', articleRouters.delete);
app.use('/system/article/edit', articleRouters.edit);
app.use('/system/article/search', articleRouters.search);

// 评论功能接口
const commentRouters = require('./routers/system/comment');
app.use('/system/comment/search', commentRouters.search);
app.use('/system/comment/delete', commentRouters.delete);

// 统计功能接口
const statisticsRouters = require('./routers/system/statistics');
app.use('/system/statistics/getStatistics', statisticsRouters.getStatistics);
app.use('/system/statistics/getArticleRanking', statisticsRouters.getArticleRanking);

/**
 * 前端接口
 */

// 用户接口
app.use('/app/user/search', userRouters.search);

// 类型接口
app.use('/app/type/search', typeRouters.search);

// 文章功能接口
const appArticleRouters = require('./routers/app/article');
app.use('/app/article/getArticleDetail', appArticleRouters.getArticleDetail);
app.use('/app/article/searchArticle', appArticleRouters.searchArticle);
app.use('/app/article/searchArticleForType', appArticleRouters.searchArticleForType);
app.use('/app/article/articlesLikes', appArticleRouters.articlesLikes);

// 评论

const appCommentRouters = require('./routers/app/comment');
app.use('/app/comment/add', appCommentRouters.add);
app.use('/app/comment/search', appCommentRouters.search);

app.listen(8081, () => {
    console.log('server start');
})